﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;

namespace ThresholdAssistant.Http
{
    /// <summary>
    /// Polly管理工具类
    /// </summary>
    public static class PollyManager
    {
        /// <summary>
        /// 配置信息选项
        /// </summary>
        public static PolicyOptions Config;

        /// <summary>
        /// 添加Polly重试客户端
        /// </summary>
        /// <param name="services"></param>
        public static IServiceCollection AddPollyClientUtil(this IServiceCollection services)
        {
            services.AddPollyClientUtil(new PolicyOptions());
            return services;
        }

        /// <summary>
        /// 添加Polly重试客户端
        /// </summary>
        /// <param name="services"></param>
        /// <param name="options"></param>
        public static IServiceCollection AddPollyClientUtil(this IServiceCollection services, PolicyOptions options)
        {
            Config = options ?? throw new ArgumentNullException("请设置配置信息参数");
            services.AddHttpClientUtil();
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            return services;
        }
    }
}